package com.egoonet.dao;

import com.egoonet.dao.projections.PostIdAndTitle;
import com.egoonet.domain.Post;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;

import java.util.List;

/**
 *
 * Created by wchi on 2017/4/20.
 */
public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificationExecutor<Post>, QueryDslPredicateExecutor<Post> {
    /**
     * 用于测试两个表连接查询
     * @param userName
     * @return
     */
    @Cacheable(value = "post", key = "#p0")
    List<Post> findByCreator_UserName(String userName);

    /**
     * 用于测试多条件表连接查询
     * @param userName
     * @param isdn
     * @return
     */
    List<Post> findByCreator_UserNameAndIsdnGreaterThan(String userName, Integer isdn);

    /**
     * 测试Like关键字
     * @param title
     * @return
     */
    List<Post> findByTitleLike(String title);

    /**
     * 测试Containing关键字
     * @param title
     * @return
     */
    List<Post> findByTitleContaining(String title);

    /**
     * 测试JPA中的projections方法来获取部分列值
     * @return
     */
    @Query(value = "select id,title from post", nativeQuery = true)
    List<PostIdAndTitle> findAllIdAndTitle();

    List<PostIdAndTitle> findByIsdn(Integer isdn);
}
